package com.boot.app.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boot.api.ApiResult;
import com.boot.app.system.domain.SystemCode;
import com.boot.app.system.service.ISystemCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("system/code")
public class SystemCodeController {

    private final ISystemCodeService service;

    @Autowired
    public SystemCodeController(ISystemCodeService service){
        this.service = service;
    }

    @GetMapping("{id}")
    public ApiResult<SystemCode> getCodeGroup(@PathVariable("id") long id){
        SystemCode systemCode = service.getById(id);
        return ApiResult.success(systemCode);
    }

    @GetMapping("list/{dictId}")
    public ApiResult<List<SystemCode>> getCodeListByDictId(@PathVariable String dictId){
        LambdaQueryWrapper<SystemCode> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemCode::getDictId, dictId);
        List<SystemCode> systemCodeList = service.list(wrapper);
        return ApiResult.success(systemCodeList);
    }

    @PostMapping()
    public ApiResult<?> postOrPutCode(@RequestBody SystemCode systemCode){
        service.saveOrUpdate(systemCode);
        return ApiResult.success();
    }

    @DeleteMapping("{id}")
    public ApiResult<?> deleteCode(@PathVariable("id") long id){
        service.removeById(id);
        return ApiResult.success();
    }

}
